package com.donger.business.upms.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.donger.business.upms.service.SmsService;
import com.donger.common.core.constant.CommonConstants;
import com.donger.common.core.constant.SecurityConstants;
import com.donger.common.data.service.Global;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 7326
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 验证码生成
     * @param mobile
     * @return
     */
    @Override
    public String sendSmsCode(String mobile) {
        String code = null;

        code = (String) redisTemplate.opsForValue().get(CommonConstants.DEFAULT_PHONE_KEY + mobile);
        if(code  == null){
            code = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
            log.debug("手机号生成验证码成功:{},{}", mobile, code);
        }



        String time = Global.getConfig("sys.account.validCodeTimeout", "10");
        redisTemplate.opsForValue().set(
                CommonConstants.DEFAULT_PHONE_KEY + mobile
                , code, Long.parseLong(time), TimeUnit.SECONDS);
        return code;
    }

    /**
     * 校验验证码
     * @param phone
     * @param code
     * @return
     */
    @Override
    public boolean validate(String phone, String code) {
        return false;
    }
}
